package servlet;

import biz.BookService;
import biz.BookServiceImpl;
import com.alibaba.fastjson.JSON;
import pojo.Book;
import utils.Constants;
import utils.Page;
import vo.BookVO;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "BookServlet", value = "/getBooks")
public class BookServlet extends HttpServlet {

    BookService bookService =null;
    public BookServlet() {
        bookService =new BookServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String opr = request.getParameter("opr");

        try {
            if (null==opr || "".equals(opr)) opr="list";

            if ("list".equals(opr))
            {
                try {
                    // 分页
                    Page page =new Page();

                    Integer _pageIndex= 1;
                    String pageIndex= request.getParameter("pageIndex");// 1;//当前页

                    if (null==pageIndex || "".equals(pageIndex)){
                        _pageIndex= 1;
                    }

                    _pageIndex= Integer.valueOf(pageIndex);  //记得把得到的参数给 _index

                    if (_pageIndex<=0){
                        _pageIndex= 1;
                    }

                    page.setCurrPageNo(_pageIndex);//当前页
                    page.setPageSize(Constants.PAGE_SIZE);

                    BookVO book_params = new BookVO();

                    String bookName =request.getParameter("bookName"); //书籍名称
                    if (null != bookName){
                          book_params.setBookName(bookName);
                    }

                    Integer sortId =Integer.parseInt(
                            request.getParameter("sortId")==null?"0":request.getParameter("sortId"));
                    book_params.setSort_id(sortId); ;

                    bookService.getBooList(book_params,page); //执行查询
                    if (_pageIndex>=page.getTotalPageCount()){
                        //如果当前页>= 最后一页, 则显示最后一页的数据
                        _pageIndex =page.getTotalPageCount();
                    }

                    //模拟jsp页面实现查全部
                   // page.getList().forEach(book-> System.out.println(book));
                  //  System.out.println("共"+page.getTotalCount()+"笔数据,当前第"+ page.getCurrPageNo()+"/"+page.getTotalPageCount());
                    out.print(JSON.toJSONString(page));


                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }


            }
            else if("addBook".equals(opr)){ //新增

                //获取参数  //("bookIsbn"),  ("bookPub"),  ("bookTotal"),
                //封装对象
                Book book = new Book(request.getParameter("isbn"),
                        request.getParameter("bookName"),
                        request.getParameter("bookAuthor"),
                        request.getParameter("bookPublisher"),
                        Integer.valueOf(request.getParameter("bookSort")),
                        Integer.valueOf(request.getParameter("bookNums")),
                        request.getParameter("bookPubLocation"),
                        request.getParameter("bookPubYear"));
                //调用方法执行修改
                int count = bookService.modifyBook(book,"insert");
                //返回执行结果
                out.print(JSON.toJSONString(count));
            }else if("updateBookPhoto".equals(opr)){
                //修改 图片

                String isbn =request.getParameter("isbn");

                //调用查询方法按id ,查出来这笔信息
                BookVO book_params = new BookVO();
                book_params.setBookIsbn(isbn);
                Page page =new Page();
                page.setCurrPageNo(1);
                bookService.getBooList(book_params,page);  //李灵


                //把按isbn号查出来的book,的photo字段,改成前端处理的photo的结果
                String photo_url =request.getParameter("photo");
                Book book= (Book) page.getList().get(0);
                book.setBookPhoto(photo_url);

                bookService.modifyBook(book,"update");
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
